package com.feige.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.constant.Constants;
import com.feige.dao.PrivateChatMsgMapper;
import com.feige.pojo.PrivateChatMsg;
import com.feige.pojo.vo.ChatMsgVo;
import com.feige.service.PrivateChatMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 私聊消息表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-12-26
 */
@Service
public class PrivateChatMsgServiceImpl extends ServiceImpl<PrivateChatMsgMapper, PrivateChatMsg> implements PrivateChatMsgService {

    @Resource
    private PrivateChatMsgMapper privateChatMsgMapper;

    @Override
    public List<ChatMsgVo> queryChatMsg(Integer pageNum, String senderId, String receiverId,Long id) {
        HashMap<String, Object> map = new HashMap<>(10);
        map.put("pageNum",(pageNum - 1) * Constants.CHAT_MSG_PAGE_SIZE);
        map.put("senderId",senderId);
        map.put("receiverId",receiverId);
        map.put("pageSize", Constants.CHAT_MSG_PAGE_SIZE);
        map.put("id",id);
        List<ChatMsgVo> chatMsgVos = privateChatMsgMapper.queryChatMsg(map);
        return chatMsgVos.stream().map(chatMsgVo -> {
            if (StrUtil.isNotBlank(chatMsgVo.getAvatar())){
                chatMsgVo.setAvatarType(1);
            }else {
                if (StrUtil.isBlank(chatMsgVo.getName())){
                    chatMsgVo.setName("课");
                }
                chatMsgVo.setAvatarType(3);
            }
            return chatMsgVo;
        }).collect(Collectors.toList());
    }

    @Override
    public int queryChatMsgTotal(String senderId, String receiverId,Long id) {
        HashMap<String, Object> map = new HashMap<>(10);
        map.put("senderId",senderId);
        map.put("receiverId",receiverId);
        map.put("id",id);
        return privateChatMsgMapper.queryChatMsgTotal(map);
    }

    @Override
    public int batchUpdateChatMsg(List<String> msgIdList) {
        return privateChatMsgMapper.batchUpdateChatMsg(msgIdList);
    }

    @Override
    public List<ChatMsgVo> queryUnsignMsg(String receiverId) {
        List<ChatMsgVo> chatMsgVos = privateChatMsgMapper.queryUnsignMsg(receiverId);
        return chatMsgVos.stream().peek(chatMsgVo -> {
            if (StrUtil.isNotBlank(chatMsgVo.getAvatar())){
                chatMsgVo.setAvatarType(1);
            }else {
                if (StrUtil.isBlank(chatMsgVo.getName())){
                    chatMsgVo.setName("课");
                }
                chatMsgVo.setAvatarType(3);
            }
        }).collect(Collectors.toList());
    }

}
